package com.example.algorithm.interview;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 荣耀社招笔试题
 *
 * @description 最大利润，通过80%用例
 * @date 2023-07-15 11:11
 */
public class MaxProfit {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        // 每种水果成本
        String[] m = s1.split(",");
        String s2 = in.nextLine();
        // 每种水果卖出价格
        String[] n = s2.split(",");
        // 本金
        int k = in.nextInt();

        List<Tmp> mList = new ArrayList<>();
        for (int i = 0; i < m.length; i++) {
            Integer mi = Integer.valueOf(m[i]);
            Integer ni = Integer.valueOf(n[i]);
            Tmp tmp = new Tmp(mi, ni - mi);
            mList.add(tmp);
        }

        while (true) {
            boolean change = false;
            for (Tmp tmp : mList) {
                if (!tmp.isVisit() && k >= tmp.getAmount()) {
                    // 买得起
                    k += tmp.getMoney();
                    tmp.setVisit(true);
                    change = true;
                }
            }
            // 一轮下来没有更新了，说明都买不起了
            if (!change) {
                break;
            }
        }

        // 最后能获得多少钱
        System.out.println(k);
    }

    @Getter
    @Setter
    private static class Tmp {
        private Integer amount;
        private boolean visit;

        // 每种水果能赚多少
        private Integer money;

        public Tmp(Integer amount, Integer money) {
            this.amount = amount;
            this.money = money;
        }
    }
}
